//
//  AlignCategoriesSystem.cpp
//  QuizMaster
//
//  Created by Jeppe Nielsen on 3/22/15.
//  Copyright (c) 2015 Jeppe Nielsen. All rights reserved.
//

#include "AlignCategoriesSystem.hpp"


void AlignCategoriesSystem::Initialize() {
    AddComponent<Transform>();
    AddComponent<CategoryComponent>();
    objectsNeedsAlignment = false;
}

void AlignCategoriesSystem::ObjectAdded(GameObject* object) {
    objectsNeedsAlignment = true;
}

void AlignCategoriesSystem::ObjectRemoved(GameObject *object) {

}

void AlignCategoriesSystem::Update(float dt) {
    if (!objectsNeedsAlignment) return;
    objectsNeedsAlignment = false;

    size_t count = Objects().size();
    float width = boundingBox.extends.x * 2;
    float dWidth = width / count;
    
    int counter = 0;
    for (ObjectCollection::const_iterator it = Objects().begin(); it!=Objects().end(); ++it) {
        MoveObject(*it, Vector3(-boundingBox.extends.x + dWidth * 0.5f + dWidth * counter, 3, 0));
        counter++;
    }
}

void AlignCategoriesSystem::MoveObject(GameObject *object, Vector3 position) {
    object->GetComponent<Transform>()->Position = position;
    
    CategoryComponent* category = object->GetComponent<CategoryComponent>();
    
    for (int i=0; i<category->questions.size(); i++) {
        Vector3 questionPosition = position + Vector3(0,0.5f - ((i+1)*2.0f),0);
        category->questions[i]->GetComponent<Transform>()->Position = questionPosition;
    }
    
    
    
}


